package org.delmore.eraser;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * 橡皮檫功能，类似刮刮乐效果
 * 
 * @author delmore 20110827
 */
public class TextEraser extends TextView
{

	private float TOUCH_TOLERANCE = 1f;
	// 填充距离，使线条更自然，柔和,值越小，越柔和。

	// private final int bgColor;

	private Bitmap mBitmap;
	private Canvas mCanvas;
	private Paint mPaint;
	private Path mPath;
	private float mX, mY;

	int bgColor = 0XFFCECECE;

	public TextEraser(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}

	private void init()
	{
		mPath = new Path();
		mPaint = new Paint();
		mPaint.setAlpha(0);
		// 画笔划过的痕迹就变成透明色了
		// mPaint.setColor(Color.BLACK); // 此处不能为透明色
		// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
		// 或者
		mPaint.setAlpha(0);
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));

		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圆角
		mPaint.setStrokeCap(Paint.Cap.ROUND); // 后圆角
		mPaint.setStrokeWidth(20); // 笔宽
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		mCanvas.drawPath(mPath, mPaint);
		canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		mPath.reset();
		mBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mCanvas.drawColor(bgColor);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		switch (event.getAction())
		{
			case MotionEvent.ACTION_DOWN : // 触点按下
				// touchDown(event.getRawX(),event.getRawY());
				touchDown(event.getX(), event.getY());
				invalidate();
				break;
			case MotionEvent.ACTION_MOVE : // 触点移动
				touchMove(event.getX(), event.getY());
				invalidate();
				break;
			case MotionEvent.ACTION_UP : // 触点弹起
				touchUp(event.getX(), event.getY());
				invalidate();
				break;
			default :
				break;
		}
		return true;
	}

	private void touchDown(float x, float y)
	{
		mPath.reset();
		mPath.moveTo(x, y);
		mPath.lineTo(x + 0.1f, y + 0.1f);
		mX = x;
		mY = y;
	}

	private void touchMove(float x, float y)
	{
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
		{
			// mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
			mPath.lineTo(x, y);
			mX = x;
			mY = y;
		}
	}

	private void touchUp(float x, float y)
	{
		mPath.lineTo(x, y);
		// mCanvas.drawPath(mPath, mPaint);
		// mPath.reset();
	}

}
